package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.Question;

public class QuestionDao implements BaseDao {

	private final String ADD_QUESTION = "INSERT INTO question (user_id,category_id,text,title,time,likes) VALUES (?,?,?,?,?,?)";
	private final String REMOVE_QUESTION = "DELETE FROM question WHERE id = ?";
	private final String GET_BY_ID = "SELECT * FROM question WHERE id = ?";
	private final String UPDATE_QUESTION = "UPDATE question SET user_id=?, category_id=?,text=?,title=?, time=?, likes=? WHERE id = ?";
	private final String UPDATE_LIKES = "UPDATE question SET likes=? WHERE id=?";
	private static final Logger LOG = Logger.getLogger("stdout");

	@Override
	public <T> void create(T element) {
		Question temp = (Question) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_QUESTION)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getCategoryId());
			preparedStatement.setString(3, temp.getText());
			preparedStatement.setString(4, temp.getTitle());
			preparedStatement.setTimestamp(5, temp.getTime());
			preparedStatement.setInt(6, temp.getLikes());
			preparedStatement.executeUpdate();
			LOG.info("Question added");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		Question question = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				question = Converter.<Question> fromResaultSetToObject(rs,
						Question.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) question;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_QUESTION)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
			LOG.info("Question deleted");
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}

	@Override
	public <T> void update(T element) {
		Question temp = (Question) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_QUESTION)) {
			preparedStatement.setInt(7, temp.getId());
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getCategoryId());
			preparedStatement.setString(3, temp.getText());
			preparedStatement.setString(4, temp.getTitle());
			preparedStatement.setTimestamp(5, temp.getTime());
			preparedStatement.setInt(6, temp.getLikes());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	public <T> void updateLikes(T element) {
		Question temp = (Question) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_LIKES)) {
			preparedStatement.setInt(2, temp.getId());
			preparedStatement.setInt(1, temp.getLikes());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

}
